//
//  InputContentController.m
//  QRBuildScan
//
//  Created by  on 12-1-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "InputContentController.h"
#import "ShowQRViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "pinyin.h"
#import "AppDelegate.h"

#define TextViewTag 0x40
#define TextFeildTag 0x41
#define SearchTag 0x42
#define LocationDownTag 0x43

#define INDEX_CHAR @"indexchar" //index key name
#define INDEX_ARRAY @"indexarr"
@implementation DisplayMap
@synthesize coordinate,title,subtitle;
-(void)dealloc{
    [title release];
    [super dealloc];
}
@end

@implementation InputContentController
@synthesize m_ShowType;
@synthesize m_CreateQR;

-(void)setM_ShowType:(ContentType)p_ShowType{
    m_ShowType = p_ShowType;
    p_QRContent = [[NSMutableString alloc] init];
    m_CreateQR = [UIButton buttonWithType:UIButtonTypeCustom];
    [m_CreateQR setImage:[UIImage imageNamed:@"createqr.png"] forState:UIControlStateNormal];
    m_CreateQR.frame =CGRectMake(254, 7, 60, 30);
    [m_CreateQR addTarget:self action:@selector(createQRImage) forControlEvents:UIControlEventTouchUpInside];
    [m_CreateQR retain];
    if (m_ShowType == Type_Mecard) {
        p_Characters= [[NSMutableArray alloc] init];
        p_ContactArr = [[NSMutableArray alloc] init];
        p_SrcContactArr =[[NSMutableArray alloc] init];

        
        ABAddressBookRef addressBook = ABAddressBookCreate();
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);  
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);  
        
        for (int i = 0; i < nPeople; i++) {
            NSMutableDictionary* tOneDic = [NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);  
            CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);  
            CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            ABRecordID recId = ABRecordGetRecordID(ref);
            if (firstName == nil) {
                firstName=(CFStringRef)@"";
            }
            if (lastName == nil) {
                lastName=(CFStringRef)@"";
            }
            [tOneDic setObject:(NSString*)firstName forKey:@"fname"];
            [tOneDic setObject:(NSString*)lastName forKey:@"lname"];
            [tOneDic setObject:[NSNumber numberWithInt:recId] forKey:@"id"];
            CFRelease(firstName);  
            CFRelease(lastName);  
              
            
            ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            //所有ABMutableMultiValueRef这样的引用的东西都是这样一个元组（id，label，value）
            multiPhone = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhone); i++) {
                CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(multiPhone, i);
                CFStringRef numberRef = ABMultiValueCopyValueAtIndex(multiPhone, i);
                //可以通过元组的label来判定这个电话是哪种电话，比如下面就包括：主电话，手机，工作传真
                if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMobileLabel]){
                    if (numberRef == nil) {
                        numberRef=(CFStringRef)@"";
                    }
                    [tOneDic setObject:(NSString*)numberRef forKey:@"phonenum"];
                }else if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneIPhoneLabel]){
                    if (numberRef == nil) {
                        numberRef=(CFStringRef)@"";
                    }
                    [tOneDic setObject:(NSString*)numberRef forKey:@"iphone"];
                }
                CFRelease(labelRef);
                CFRelease(numberRef);
            }
            CFRelease(multiPhone);
            [p_SrcContactArr addObject:tOneDic];
            NSLog(@"%@",[tOneDic description]);
        }  
        CFRelease(allPeople);
        CFRelease(addressBook);
        
        
        [self liteDataArr:nil];
        
        p_MecardTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, ScreenHeight-44-90) style:UITableViewStylePlain];
        p_MecardTable.delegate = self;
        p_MecardTable.dataSource = self;
        [self.view addSubview:p_MecardTable];
        UISearchBar* tSearchBar =[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        tSearchBar.barStyle = UIBarStyleBlack;
        tSearchBar.delegate =self;
        tSearchBar.tag =SearchTag;
        [self.view addSubview:tSearchBar];
        [tSearchBar release];
        
        p_OverButton = [UIButton buttonWithType:UIButtonTypeCustom];
        p_OverButton.frame =CGRectMake(0, 40, ScreenWidth, ScreenHeight-44-90);
        [p_OverButton addTarget:self action:@selector(blackViewOver) forControlEvents:UIControlEventTouchUpInside];
        [p_OverButton retain];
    }else if(m_ShowType == Type_Position){
        UISearchBar* tSearchBar =[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        tSearchBar.barStyle = UIBarStyleBlack;
        tSearchBar.delegate =self;
        tSearchBar.tag =SearchTag;
        [self.view addSubview:tSearchBar];
        [tSearchBar release];
        
        mapView= [[MKMapView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, ScreenHeight-44-90)];
        mapView.showsUserLocation=YES;
        mapView.delegate =self;
        
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器 
        locationManager.delegate=self;//设置代理 
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别 
        locationManager.distanceFilter=1000.0f;//设置距离筛选器 
        [locationManager startUpdatingLocation];//启动位置管理器 
        MKCoordinateSpan theSpan; 
        //地图的范围 越小越精确 
        theSpan.latitudeDelta=0.05; 
        theSpan.longitudeDelta=0.05; 
        MKCoordinateRegion theRegion; 
        theRegion.center=[[locationManager location] coordinate]; 
        theRegion.span=theSpan; 
        [mapView setRegion:theRegion]; 
        [locationManager release]; 
        [self.view addSubview:mapView];
        
        p_OverButton = [UIButton buttonWithType:UIButtonTypeCustom];
        p_OverButton.frame =CGRectMake(0, 40, ScreenWidth, ScreenHeight-44-90);
        [p_OverButton addTarget:self action:@selector(blackViewOver) forControlEvents:UIControlEventTouchUpInside];
        [p_OverButton retain];
        
        p_SCDownBar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"下标题_灰.png"]];
        p_SCDownBar.frame =CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
        p_SCDownBar.userInteractionEnabled =YES;
        p_SCDownBar.tag = LocationDownTag;
        
        
        
        UIButton* tBackLocation = [UIButton buttonWithType:UIButtonTypeCustom];
        [tBackLocation setImage:[UIImage imageNamed:@"foot_01.png"] forState:UIControlStateNormal];
        tBackLocation.frame =CGRectMake(260, 6, 40, 40);
        [tBackLocation addTarget:self action:@selector(backSelfLocation) forControlEvents:UIControlEventTouchUpInside];
        [p_SCDownBar addSubview:tBackLocation];
        

        
    }else if(m_ShowType == Type_PhoneNum || m_ShowType == Type_Email || m_ShowType == Type_WWWURL){
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tablebg.png"]]];
        
        UIImageView* tViewBG = [[UIImageView alloc] initWithFrame:CGRectMake(20, 62, 280, 44)];
        [tViewBG setImage:[UIImage imageNamed:@"onecellg.png"]];
        [self.view addSubview:tViewBG];
        [tViewBG release];
        
        UILabel* tInputLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];
        [tInputLabel setTextColor:[UIColor colorWithRed:29/255.f green:31/255.f blue:36/255.f alpha:1.0f]];
        [tInputLabel setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:tInputLabel];
        if (m_ShowType == Type_PhoneNum) {
            tInputLabel.text =@"输入电话号码：";
        }else if(m_ShowType == Type_Email){
            tInputLabel.text =@"输入电邮地址：";
        }else if(m_ShowType == Type_WWWURL){
            tInputLabel.text =@"输入网页地址：";
        }
        
        UITextField* tInputApp =[[UITextField alloc] initWithFrame:CGRectMake(26, 75, 270, 34)];
        tInputApp.tag =TextFeildTag;
        [self.view addSubview:tInputApp];
        [tInputApp release];
        [tInputApp becomeFirstResponder];
    }else if(m_ShowType == Type_Text){
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tablebg.png"]]];
        
        CALayer* tSeeLayer = [CALayer layer];
        [tSeeLayer setBackgroundColor:[UIColor whiteColor].CGColor];
        tSeeLayer.frame =CGRectMake(6, 10, ScreenWidth-12, 210);
        tSeeLayer.borderColor =[UIColor grayColor].CGColor;
        tSeeLayer.borderWidth =1.0f;
        tSeeLayer.cornerRadius =8.0f;
        tSeeLayer.masksToBounds =YES;
        [self.view.layer addSublayer:tSeeLayer];
    
        UITextView* tInputApp =[[UITextView alloc] initWithFrame:CGRectMake(10,12,300, 210)];
        tInputApp.tag =TextViewTag;
        [tInputApp setFont:[UIFont systemFontOfSize:16.f]];
        [tInputApp setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:tInputApp];
        [tInputApp release];
        [tInputApp becomeFirstResponder];
        
        for (int i=0; i<8; i++) {
            CALayer* tlineLayer = [CALayer layer];
            tlineLayer.frame =CGRectMake(10, 28+i*22, 280, 1);
            [tlineLayer setBackgroundColor:[UIColor grayColor].CGColor];
            [tInputApp.layer addSublayer:tlineLayer];
        }
    }else if(m_ShowType == Type_SMS){
        
    }
}

-(void) viewWillAppear:(BOOL)animated{
    AppDelegate* tDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tDelegate.window addSubview:p_SCDownBar];
}
-(void) viewWillDisappear:(BOOL)animated{
    [p_SCDownBar removeFromSuperview];
}

-(void) dealloc{
    [p_SrcContactArr release];
    [p_Characters release];
    [p_MecardTable release];
    [p_ContactArr release];
    [p_SCDownBar release];
    [p_OverButton release];
    [mapView release];
    [m_CreateQR removeFromSuperview];
    [m_CreateQR release];
    [p_QRContent release];
    [super dealloc];
}


-(void) createQRImage{
    [p_QRContent setString:@""];
    if(m_ShowType == Type_PhoneNum || m_ShowType == Type_Email || m_ShowType == Type_WWWURL){
        UITextField* tInputApp =(UITextField*)[self.view viewWithTag:TextFeildTag];
        if (tInputApp.text.length >=150) {
            UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"内容数据过长！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [tAlert show];
            [tAlert release];
            return;
        }else{
            [p_QRContent setString:tInputApp.text];
        }
    }else if(m_ShowType == Type_Text){
        UITextView* tInputApp =(UITextView*)[self.view viewWithTag:TextViewTag];
        if (tInputApp.text.length >=150) {
            UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"内容数据过长！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [tAlert show];
            [tAlert release];
            return;
        }else{
            [p_QRContent setString:tInputApp.text];
        }
    }else if(m_ShowType == Type_Position){
        UISearchBar* tSearchBar =(UISearchBar*)[self.view viewWithTag:SearchTag];
        if (tSearchBar.text.length >=150) {
            UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"内容数据过长！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [tAlert show];
            [tAlert release];
            return;
        }else{
            if (tSearchBar.text == nil) {
                tSearchBar.text =@"";
            }
            [p_QRContent setString:tSearchBar.text];
        }
    }
    if (p_QRContent.length <= 0) {
        UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"内容不可为空！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [tAlert show];
        [tAlert release];
        return;
    }
    NSLog(@"%@",p_QRContent);
    ShowQRViewController* tNextVC = [[ShowQRViewController alloc] init];
    tNextVC.m_TranStr =p_QRContent;
    [self.navigationController pushViewController:tNextVC animated:YES];
    [tNextVC release];
}


-(void) blackViewOver{
    UISearchBar* tSBar =(UISearchBar*)[self.view viewWithTag:SearchTag];
    [tSBar resignFirstResponder];
    [p_OverButton removeFromSuperview];
}

-(void) backSelfLocation{
    mapView.showsUserLocation=YES;
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器 
    locationManager.delegate=self;//设置代理 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别 
    locationManager.distanceFilter=1000.0f;//设置距离筛选器 
    [locationManager startUpdatingLocation];//启动位置管理器 
    MKCoordinateSpan theSpan; 
    //地图的范围 越小越精确 
    theSpan.latitudeDelta=0.05; 
    theSpan.longitudeDelta=0.05; 
    MKCoordinateRegion theRegion; 
    theRegion.center=[[locationManager location] coordinate]; 
    theRegion.span=theSpan; 
    [mapView setRegion:theRegion]; 
    [locationManager release]; 
    [p_OverButton removeFromSuperview];
}

#pragma -
#pragma UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{

    [self.view addSubview:p_OverButton];   
    
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if (m_ShowType == Type_Mecard){
        [self liteDataArr:searchBar.text];
    }else{
        // 查詢經緯度
        NSString *output = @"csv";
        NSString *key = @"YouKey";
        NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=%@&key=%@",searchBar.text,output,key];
        NSString *encodingStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:encodingStr];
        
        NSString *retstr =[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"retstr =%@",retstr);
        NSArray *resultArray = [retstr componentsSeparatedByString:@","];
        
        
        double latitude = [[resultArray objectAtIndex:2] doubleValue];
        double longitude = [[resultArray objectAtIndex:3] doubleValue];
        
        
        MKCoordinateSpan theSpan; 
        //地图的范围 越小越精确 
        theSpan.latitudeDelta=0.05; 
        theSpan.longitudeDelta=0.05; 
        MKCoordinateRegion theRegion;
        CLLocation* tLLoca = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
        theRegion.center=[tLLoca coordinate]; 
        theRegion.span=theSpan;
        DisplayMap *ann = [[DisplayMap alloc] init];
        ann.title = searchBar.text;
        ann.subtitle = @"查询地";
        mapView.showsUserLocation =YES;
        [mapView setRegion:theRegion];
        ann.coordinate = theRegion.center;
        [mapView addAnnotation:ann];
        [ann release];
    }
    [searchBar resignFirstResponder];
    [p_OverButton removeFromSuperview]; 
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    }
    else {
//        [mapView.userLocation setTitle:@"欧陆经典"];
//        [mapView.userLocation setSubtitle:@"vsp"];
    }
    return pinView;
}

-(void) liteDataArr:(NSString*)key{
    
    [p_Characters removeAllObjects];
    for (char c='A'; c<= 'Z'; c++) {
        [p_Characters addObject:[NSString stringWithFormat:@"%c",c]];
    }
    [p_Characters addObject:@"#"];
    
    [p_ContactArr removeAllObjects];
    for (char c ='a'; c<='z'; c++) {
        NSMutableDictionary*  tIndexDic =[[NSMutableDictionary alloc] init];
        [tIndexDic setObject:[NSString stringWithFormat:@"%c",c] forKey:INDEX_CHAR];
        NSMutableArray* tHaveArr =[[NSMutableArray alloc] init];
        [tIndexDic setObject:tHaveArr forKey:INDEX_ARRAY];
        [tHaveArr release];
        [p_ContactArr addObject:tIndexDic];
        [tIndexDic release];
    }
    NSMutableDictionary*  tIndexDic =[[NSMutableDictionary alloc] init];
    [tIndexDic setObject:@"#" forKey:INDEX_CHAR];
    NSMutableArray* tHaveArr =[[NSMutableArray alloc] init];
    [tIndexDic setObject:tHaveArr forKey:INDEX_ARRAY];
    [tHaveArr release];
    [p_ContactArr addObject:tIndexDic];
    [tIndexDic release];
    
    for (NSDictionary* tPDic in p_SrcContactArr) {
        NSString* tStrName =[NSString stringWithFormat:@"%@%@",[tPDic objectForKey:@"fname"],[tPDic objectForKey:@"lname"]];
        NSLog(@"%@",tStrName);
        if (key != nil) {
            if ([tStrName rangeOfString:key].location ==NSNotFound) {
                continue;
            }
        }
        if (tStrName.length > 0) {
            char frist = pinyinFirstLetter(pinyinFirstLetter([tStrName characterAtIndex:0]));
            if (frist < 97) {
                frist+= 32;
            }
            NSLog(@"index =%d",frist -97);
            if (frist -97 < 0) {
                NSMutableDictionary* tParentDic = [p_ContactArr lastObject];
                NSMutableArray* tAddArray =[tParentDic objectForKey:INDEX_ARRAY];
                [tAddArray addObject:tPDic];
                [tParentDic setObject:tAddArray forKey:INDEX_ARRAY];
            }else{
                NSMutableDictionary* tParentDic = [p_ContactArr objectAtIndex:frist -97];
                NSMutableArray* tAddArray =[tParentDic objectForKey:INDEX_ARRAY];
                [tAddArray addObject:tPDic];
                [tParentDic setObject:tAddArray forKey:INDEX_ARRAY];
            }
        }else{
            NSMutableDictionary* tParentDic = [p_ContactArr lastObject];
            NSMutableArray* tAddArray =[tParentDic objectForKey:INDEX_ARRAY];
            [tAddArray addObject:tPDic];
            [tParentDic setObject:tAddArray forKey:INDEX_ARRAY];
        }
    }
    for (int i=0;i<[p_ContactArr count];i++) {
        NSMutableDictionary* dic = [p_ContactArr objectAtIndex:i];
        NSMutableArray* tAddArray =[dic objectForKey:INDEX_ARRAY];
        if ([tAddArray count] <= 0) {
            [p_ContactArr removeObject:dic];
            [p_Characters removeObjectAtIndex:i];
            i--;
        }
    }
    [p_MecardTable reloadData];
}

#pragma -
#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = [indexPath row];
    int section = [indexPath section];
    [p_QRContent setString:@""];
    NSDictionary* tItemDic = [p_ContactArr objectAtIndex:section];
    NSArray* tSectionArrayData =[tItemDic objectForKey:INDEX_ARRAY];
    NSDictionary* tPersonDic = [tSectionArrayData objectAtIndex:row];
    NSString* tNameStr = [NSString stringWithFormat:@"%@%@",[tPersonDic objectForKey:@"fname"],[tPersonDic objectForKey:@"lname"]];
    [p_QRContent appendString:[NSString stringWithFormat:@"姓名：%@",tNameStr]];
    NSString* tPhoneStr = [NSString stringWithFormat:@"%@ %@",[tPersonDic objectForKey:@"phonenum"],[tPersonDic objectForKey:@"iphone"]];
    [p_QRContent appendString:[NSString stringWithFormat:@" 号码：%@",tPhoneStr]];
    NSLog(@"%@",p_QRContent);
    if (p_QRContent.length <= 0) {
        UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"内容不可为空！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [tAlert show];
        [tAlert release];
        return;
    }
    ShowQRViewController* tNextVC = [[ShowQRViewController alloc] init];
    tNextVC.m_TranStr =p_QRContent;
    [self.navigationController pushViewController:tNextVC animated:YES];
    [tNextVC release];
}
#pragma -
#pragma UITableViewDataSource
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = [indexPath row];
    int section = [indexPath section];
    
    static NSString* rootcell =@"rootcell";
    UITableViewCell* cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:rootcell];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rootcell] autorelease];
        
        [cell.textLabel setFont:[UIFont systemFontOfSize:14.f]];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    }
    
    int tScanIndex = 0;
    for (int i=0; i<section; i++) {
        NSMutableDictionary* tParentDic = [p_ContactArr objectAtIndex:i];
        NSMutableArray* tAddArray =[tParentDic objectForKey:INDEX_ARRAY];
        tScanIndex += [tAddArray count];
    }
    
    
    NSDictionary* tItemDic = [p_ContactArr objectAtIndex:section];
    NSArray* tSectionArrayData =[tItemDic objectForKey:INDEX_ARRAY];
    NSDictionary* tPersonDic = [tSectionArrayData objectAtIndex:row];
    NSString* tNameStr = [NSString stringWithFormat:@"%@%@",[tPersonDic objectForKey:@"fname"],[tPersonDic objectForKey:@"lname"]];
    cell.textLabel.text = tNameStr;
    [cell.textLabel setTextColor:[UIColor blackColor]];
    
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([p_ContactArr count] > 0) {
        NSMutableDictionary* tSectionDic = [p_ContactArr objectAtIndex:section];
        NSArray* tContentArray =[tSectionDic objectForKey:INDEX_ARRAY];
        return [tContentArray count];
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([p_ContactArr count] > 0) {
        return [p_ContactArr count];
    }
    return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [p_Characters objectAtIndex:section];
}

@end
